//
//  BKCMBuyCarSectionView.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKCMBuyCarSectionView.h"
#import "BKSCCommon.h"
#import "BKCMGoodsDetailModel.h"

@interface BKCMBuyCarSectionView ()


@end

@implementation BKCMBuyCarSectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = COLOR_03;
        self.userInteractionEnabled = YES;
        
        _bgView = [UIView new];
        _bgView.backgroundColor = RGBCOLOR(251, 251, 251, 1.0);
        [self addSubview:_bgView];
        
        //2016年05月27日22:31:46
        _sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sectionButton setBackgroundColor:[UIColor clearColor]];
        [_sectionButton setBackgroundImage:[UIImage imageNamed:@"BKSC_select_image"] forState:UIControlStateSelected];
        [_sectionButton setBackgroundImage:[UIImage imageNamed:@"BKSC_unselect_image"] forState:UIControlStateNormal];
        _sectionButton.layer.cornerRadius =9.5f;//8---12
        _sectionButton.layer.masksToBounds = YES;
        
        
        _sectionButton_Big = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sectionButton_Big setBackgroundColor:[UIColor clearColor]];
        [_sectionButton_Big addTarget:self action:@selector(sectionBUttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_bgView addSubview:_sectionButton];
        [_bgView addSubview:_sectionButton_Big];
        
        _sectionLabel = [UILabel new];
        [_sectionLabel setText:@"----"];
        [_sectionLabel setTextColor:RGBCOLOR(64, 64, 64, 1.0)];
        [_sectionLabel setFont:[UIFont systemFontOfSize:14]];
        [_sectionLabel setTextAlignment:NSTextAlignmentLeft];
        [_bgView addSubview:_sectionLabel];
        
        _sectionImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _sectionImageView.image = [UIImage imageNamed:@"BKSCCrossBorder"];
        [_sectionImageView setContentMode:UIViewContentModeScaleAspectFit];
        [_bgView addSubview:_sectionImageView];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(0);
            make.right.mas_equalTo(self.mas_right);
            make.height.mas_equalTo(@(28));
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
        [_sectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_bgView.mas_left).offset(ApplicationBackGauge);
            make.width.mas_equalTo(@(19));//18--24
            make.centerY.mas_equalTo(_bgView.mas_centerY).offset(0);
            make.height.mas_equalTo(@(19));//18--24
        }];
        [_sectionButton_Big mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_bgView.mas_left).offset(0);
            make.top.mas_equalTo(_bgView.mas_top);
            make.bottom.mas_equalTo(_bgView.mas_bottom);
            make.width.mas_equalTo(40);
        }];
        
        [_sectionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_sectionButton.mas_right).offset(8);
            make.centerY.mas_equalTo(_bgView.mas_centerY);
        }];
        
        [_sectionImageView mas_makeConstraints:^(MASConstraintMaker *make) {// 全球购
            make.left.mas_equalTo(_sectionLabel.mas_right).offset(8);
            make.width.mas_equalTo(@(41));
            make.height.mas_equalTo(@(14));
            make.centerY.mas_equalTo(_bgView.mas_centerY).offset(0);
        }];
    }
    return self;
}

-(void)sectionBUttonClick:(UIButton *)sectionButton{
    sectionButton.selected = !sectionButton.selected;
    _sectionButton.selected = sectionButton.selected;
    if ([self.sectionDelegate respondsToSelector:@selector(sectionView:sectionButtonClick:)]) {
        [self.sectionDelegate sectionView:self sectionButtonClick:sectionButton];
    }
    
}

- (BKCMBuyCarSectionView *)sectionView:(NSMutableArray *)array viewForHeaderInSection:(NSInteger)section sectionView:(BKCMBuyCarSectionView *)sectionView leftButton:(UIButton *)leftButton selectDic:(NSMutableDictionary *)mutableSelectedDictionary {
    
    BKCMRecommendGoodsModel* shopList1 = [array objectAtIndex:section];
    
    if (shopList1.global_status1 == 1) {
        sectionView.sectionImageView.hidden = NO;
        sectionView.sectionImageView.image = [UIImage imageNamed:@"BKSCCrossBorder"];
    }
    
    BOOL isAllShopcart_flag = NO;
    for (BKCMGoodsDetailModel *shop in shopList1.shopList) {
        
        if (shop.shopcart_flag) {
            isAllShopcart_flag = YES;
        }
    }
    
    if (!isAllShopcart_flag) { //section商品中全部过期；
        shopList1.isShopcart_flag = YES;
        if (leftButton.selected) {
            sectionView.sectionButton.hidden = NO;
        }else {
            sectionView.sectionButton.hidden = YES;
        }
        sectionView.sectionLabel.textColor = [BKSCCommon fromHexValue:0xB2B2B2];
        sectionView.sectionImageView.image = [UIImage imageNamed:@"BKSCUnCrossBorder"];
    }
    
    sectionView.sectionLabel.text = [NSString stringWithFormat:@"%@",shopList1.address];
    sectionView.index = section;
    sectionView.sectionButton.selected = [BKSCBuyCarTool getSectionSelectedStatu:shopList1 mutableSelectedDictionary:mutableSelectedDictionary leftButton:leftButton];
    sectionView.sectionButton_Big.selected = sectionView.sectionButton.selected;
    
    return sectionView;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
